package com.example.heizhu.appfuncore;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.request.RequestCall;

import java.io.File;

import core.android.tools.AK;
import okhttp3.Call;

/**
 * @作者: XQ
 * @创建时间：2016/8/15 16:03
 * @类说明:
 */
public class OKUploadImageView extends RelativeLayout {
    private String zcwdfileServerUrl = "http://c.img.zckj.159.net/zcwd/uploadimg.ashx?action=headimg";
    private RequestCall uploadCall;
    private StringCallback uploadCallback;

    private View progressbar_view;
    private Handler handler = new Handler();
    private ImageView photoView;
    private TextView text;
    private String photoPath;

    public OKUploadImageView(Context context) {
        this(context, null);
    }

    public OKUploadImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public OKUploadImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View view = inflate(getContext(), R.layout.view_okupload_imageview, this);
        progressbar_view = view.findViewById(R.id.okuploadimageview_progressbar);
        photoView = (ImageView) view.findViewById(R.id.okuploadimageview_iv);
        text = (TextView) view.findViewById(R.id.okuploadimageview_tv);
    }

    float orign_height;//原始高度
    ViewGroup.LayoutParams orign_layoutParams;//原始属性

    public void uploadProgress(float progress) {
        if (orign_layoutParams == null) {
            orign_height = progressbar_view.getHeight();
            orign_layoutParams = progressbar_view.getLayoutParams();
            AK.e("orign_height=" + orign_height);
        }
        int newHeight = (int) (orign_height * (1F - progress));
        AK.e("newHeight=" + newHeight);

        if (newHeight > 0) {
            orign_layoutParams.height = newHeight;
        } else {
            orign_layoutParams.height = 0;
        }
        AK.e("orign_layoutParams.height=" + orign_layoutParams.height);

        if (progress >= 1.0F) {
            text.setVisibility(GONE);
        } else {
            progressbar_view.setLayoutParams(orign_layoutParams);
            text.setText("图片上传中 \n " + progress * 100 + "%");
        }

    }

    float progress = 0F;

    private void test() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                progress += 0.1F;
                AK.e(progress);
                uploadProgress(progress);
                if (progress < 1F) {
                    test();
                }
            }
        }, 100);
    }

    public void setImagePath(String imageFilePath) {
        this.photoPath = imageFilePath;
        photoView.setImageBitmap(BitmapFactory.decodeFile(imageFilePath));
    }

    public void upload(StringCallback callback) {
        this.uploadCallback=callback;
        if (uploadCall != null) {
            AK.d("uploading...");
            uploadCall.cancel();
            return;
        }
        File picFile = new File(photoPath);
        if (!picFile.exists()) {
            AK.e("图片文件不存在" + picFile.getAbsolutePath());
            return;
        }
        uploadProgress(0F);
        uploadCall = OkHttpUtils.post()
                .addFile("uploadfile", picFile.getName(), picFile)
                .url(zcwdfileServerUrl)
                .tag(photoPath)
                .build();

        uploadCall.execute(new StringCallback() {
            @Override
            public void inProgress(float progress, long total, int id) {
                super.inProgress(progress, total, id);
                uploadProgress(progress);
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                if (uploadCallback != null) {
                    uploadCallback.onError(call,e, i);
                }
                uploadCall = null;
                uploadCallback=null;
            }

            @Override
            public void onResponse(String s, int i) {
                if (uploadCallback != null) {
                    uploadCallback.onResponse(s, i);
                }
                uploadCall = null;
                uploadCallback=null;
            }
        });
    }
}
